package com.malbbmsomanager.management;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.malbbmsomanager.transactions.DM_BBM_InvoiceSalesOrder;
import com.malbbmsomanagerdemo.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class DM_BBM_Items_Stocks_Information extends Activity {
	private static final String TAG = null;
	private SQLiteDatabase mydb;
	private String DBNAME = "dminvoicemanagerbbm.db";
	String CurrentRefItem, strMainQOH, strVanQOH, ReOrderQty,FreeQty, strItemName;

	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(), DM_BBM_Item_Master.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_bbm_items_stocks_information);

		TextView txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Stock In Hand");
		selectCurrentItemCode();
	}

	private void selectCurrentItemCode() {
		// TODO Auto-generated method stub
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentItems = null;
		try {
			String query = "SELECT * from FItemsMasterSum";
			curCurrentItems = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentItems != null) {
			if (curCurrentItems.getCount() > 0) {
				while (curCurrentItems.moveToNext()) {
					CurrentRefItem = curCurrentItems.getString(1);
					Log.v(TAG, "CurrentRefItem XXX :" + CurrentRefItem);
					filterItemsOutstanding();
				}
			}
		}
		curCurrentItems.close();
		mydb.close();
	}

	private void filterItemsOutstanding() {
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dmitemsoustandrevisionlist);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "colitempri_1", "colitempri_2",
				"colitempri_3" };
		int[] totrdes = new int[] { R.id.item_out_name, R.id.item_out_mainqty,
				R.id.item_out_vanqty };
		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();

		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor chkItemdOutstand = null;
		try {
			/*String queryChkItemInfo = "SELECT fi.ItemCode,fi.ItemName,fi.ReOrderQty,fq.FreeQty,fq.ItemCode FROM FItems fi,fItemFreeQty fq WHERE fi.ItemCode = fq.ItemCode AND fi.ItemCode = '" + CurrentRefItem + "'";
			chkItemdOutstand = mydb.rawQuery(queryChkItemInfo, null);*/
			String queryChkItemInfo = "SELECT FQ.ItemCode ,FQ.FreeQty ,FI.ItemCode,FI.ItemName,FI.ReOrderQty FROM fItemFreeQty FQ INNER JOIN FItems FI ON FQ.ItemCode = FI.ItemCode   WHERE FQ.ItemCode = '"+ CurrentRefItem +"'";
			chkItemdOutstand = mydb.rawQuery(queryChkItemInfo, null);
			/*String queryChkItemInfo = "SELECT ItemCode ,FreeQty FROM fItemFreeQty";
			chkItemdOutstand = mydb.rawQuery(queryChkItemInfo, null);*/
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (chkItemdOutstand != null) {
			if (chkItemdOutstand.getCount() > 0) {
				while (chkItemdOutstand.moveToNext()) {
					String strItemCode = chkItemdOutstand.getString(0);
					strItemName = chkItemdOutstand.getString(3);
					ReOrderQty = chkItemdOutstand.getString(4);
					FreeQty = chkItemdOutstand.getString(1);
					Log.v(TAG, "ITEM DET :" + strItemCode + " = " + FreeQty);
				}
				HashMap<String, String> maptrdes = new HashMap<String, String>();
				maptrdes.put("colitempri_1", strItemName);
				maptrdes.put("colitempri_2", ReOrderQty);
				maptrdes.put("colitempri_3", FreeQty);

				fillMapstrdes.add(maptrdes);
				// fill in the grid_item layout
				SimpleAdapter adaptertrdes = new SimpleAdapter(this,
						fillMapstrdes,
						R.layout.dm_items_stockout_information_raw, fromtrdes,
						totrdes);
				lvtypedes.setAdapter(adaptertrdes);
			}
			/*else{
				final AlertDialog alertDialog = new AlertDialog.Builder(
						DM_BBM_Items_Stocks_Information.this).create();
				// Setting Dialog Title
				alertDialog.setTitle(this.getTitle());
				// Setting Dialog Message
				alertDialog.setMessage("Stocks Free Quantity Not Available");
				// Setting Icon to Dialog
				alertDialog.setIcon(R.drawable.warning);
				// Setting OK Button
				alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						alertDialog.dismiss();
					}
				});
				// Showing Alert Message
				alertDialog.show();
				alertDialog.setCanceledOnTouchOutside(false);
				return;
			}*/
		}
		chkItemdOutstand.close();
		mydb.close();
	}

	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}